home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Software Vault: The Gold Collection
/
Software Vault - The Gold Collection (American Databankers) (1993).ISO
/
cdr10
/
bagtag2.zip
/
BAG_UTIL.CB
next >
Wrap
Text File
|
1993-03-20
|
8KB
|
298 lines
/****************************************************************************
*
* bag_util.cb 20Mar93
*
* Misc. routines: utility functions
*
* Contains macros:
* read_marked
* getyorn
* _chk_abort
* GetTempDir
* GetIdentifier
* _emPrompt1 (from EditMaster)
*
* Copyright (c) 1990-1993 B. Goldstein -- Pequod Software
*
* Change History (see also individual entries):
* Date Who What
* ------- --- ---------------------------
* 19Apr90 BAG First draft
* 04May92 BAG Added _chk_abort
* 04Jul92 BAG Added GetTempDir
* 02Nov92 BAG Added GetIdentifier
* 19Mar93 BAG Added _emPrompt1 from EditMaster file emsup.cb
*
****************************************************************************/
/* Definitions */
#include <cbrief.h>
/***************************************************************************/
/* Externals */
string read_marked (void);
int getyorn (string msg);
int _chk_abort (~string);
string GetIdentifier (~string);
/***************************************************************************/
/****************************************************************************
*
* read_marked 18Nov92
*
* Return marked string
*
* returns null string if more than one line is marked
* leaves marking if unsuccessful
*
* If arg is non-zero, don't trim at all
*
* Change History:
* Date Who What
* ------- --- ---------------------
* 09Mar90 BAG Killed allowing multiple lines
* 19Aug92 BAG Added arg to suppress trimming leading, trailing blanks
* 18Nov92 BAG Return null string if nothing at all is marked
*
****************************************************************************/
string read_marked (~int donttrim)
{ int start_line,
end_line,
start_col,
end_col,
mark_type;
string line;
mark_type = inq_marked (start_line, start_col, end_line, end_col);
if (mark_type == 0)
return ("");
save_position ();
if (start_line != end_line)
return ("");
raise_anchor ();
/* Read the line into a string, and figure out how much */
/* of it was marked. */
if (mark_type == LINE_MARK)
start_col = 1;
move_abs (start_line, start_col);
line = read ();
//@@@ if (! donttrim)
//@@@ line = trim (ltrim (line));
/* For line marks, we want the whole line after trimming */
/* leading and trailing whitespace. For other types of marks, */
/* we have to try to return just the marked part of the line. */
if (mark_type != LINE_MARK)
{ end_col -= start_col - 1;
line = substr (line, 1, end_col);
}
if (! donttrim)
line = trim (ltrim (line));
returns (line);
restore_position ();
}
/****************************************************************************
*
* getyorn 19Apr90
*
* getyorn - Display prompt with " (Y/N)? " added and return YES or NO
*
* Change History:
* Date Who What
* 19Apr90 BAG First draft
*
****************************************************************************/
int getyorn (string msg)
{ int key;
message ("%s (Y/N)? ", msg);
while (FOREVER)
{ key = read_char () % 256;
if (key == 121 || key == 89) // 'y' || 'Y'
{ key = YES;
break;
}
else if (key == 110 || key == 78) // 'n' || 'N'
{ key = NO;
break;
}
}
/* Erase message */
message ("");
return (key);
}
/****************************************************************************
*
* _chk_abort 19Mar93
*
* Looks for any key hit, asks for confirm of abort
*
* Returns TRUE iff caller is to abort processing
*
* Change History:
* Date Who What
* ------- --- -------------
* 04May92 BAG First draft
* 19Mar93 BAG Added optional message arg
*
****************************************************************************/
int _chk_abort (~string)
{ int ch;
string msg;
if (! get_parm (0, msg))
msg = "Abort processing";
if (inq_kbd_char ())
{ ch = read_char ();
return (getyorn (msg));
}
else
return (FALSE);
}
/****************************************************************************
*
* GetTempDir 04Jul92
*
* Returns usable temporary directory, from drive letter through backslash
*
* Change History:
* Date Who What
* ------- --- -------------
* 04Jul92 BAG First draft
*
****************************************************************************/
string GetTempDir ()
{ string tmpdir;
tmpdir = trim (ltrim (inq_environment ("BTMP")));
if (tmpdir == "")
tmpdir = trim (ltrim (inq_environment ("TMP")));
if (tmpdir != "" && substr (tmpdir, strlen (tmpdir), 1) != "\\")
tmpdir += "\\";
return (tmpdir);
}
/****************************************************************************
*
* GetIdentifier 06Mar93
*
* Arg is optional string of valid identifier characters
* (if omitted, use standard C chars)
*
* Returns identifier found (or null string); leaves cursor at original
* position
*
* @@@: Cf. _grabname in bhelp.cb for another way to do this
*
* Change History:
* Date Who What
* ------- --- -------------
* 02Nov92 BAG First draft
*
****************************************************************************/
string GetIdentifier (~string)
{ string Ident, ValidChars = "a-zA-Z0-9_";
int gotword, length;
get_parm (0, ValidChars);
save_position ();
/* Is cursor on [a-zA-Z0-9_]? */
if (search_string ("["+ValidChars+"]", read (1)))
gotword = 1;
else
{ /* not on a token; is there one to leftwards? */
search_back ("<|["+ValidChars+"]");
if (search_string ("["+ValidChars+"]", read (1)))
gotword = 1;
else
gotword = 0;
}
if (gotword)
{ search_back ("<|[~"+ValidChars+"]");
if (search_string ("[~"+ValidChars+"]", read (1)))
next_char ();
Ident = read ();
length = search_string ("[~"+ValidChars+"]", Ident);
Ident = substr (Ident, 1, length-1);
}
else
Ident = "";
restore_position ();
return (Ident);
}
/****************************************************************************
*
* _emPrompt1 19Mar93
*
* Prompts for a single character response. Only certain characters
* are considered valid, others are ignored. ESC produces the old
* "Command cancelled." message.
*
* Returns 0 if cancelled, otherwise the ASCII value of the char pressed
*
* Change History:
* Date Who What
* ------- --- -------------
* 19Mar93 BAG First swiped from EditMaster emsup.cb
*
****************************************************************************/
int _emPrompt1 (string prompt, string default, string valid_chars)
{ string resp;
for (;;)
{ if (get_parm (NULL, resp, prompt, 1, default) == 0 || resp == "")
{ //@@@ _emCan ();
return (0);
}
if (search_string (resp, valid_chars))
return (atoi (resp, 0));
}
}
/************************* end of bag_util.cb file *************************/